<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>

<body>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" required lay-verify="username" placeholder="请输入标题" autocomplete="off"
                    class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码框</label>
            <div class="layui-input-inline">
                <input type="password" name="password" required lay-verify="required" placeholder="请输入密码"
                    autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">辅助文字</div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">爱好</label>
            <div class="layui-input-block">
                <input type="checkbox" name="like" title="写作" value="写作">
                <input type="checkbox" name="like" title="阅读" checked value="阅读">
                <input type="checkbox" name="like" title="跑步" value="跑步">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="男" title="男">
                <input type="radio" name="sex" value="女" title="女" checked>
            </div>
        </div>


        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">出生日期</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="birthday" name="mydate" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">头像</label>
            <div class="layui-input-block">
                <input type="file" name="poster" required accept="image/*">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">自我介绍</label>
            <div class="layui-input-block">
                <textarea id="demo" style="display: none;"></textarea>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">注册</button>

            </div>
        </div>
    </form>
    <script src="/layui/layui.js"></script>

    <script>
        //Demo
        layui.use(['form', 'laydate', 'layedit', 'jquery'], function () {
            var form = layui.form,
                laydate = layui.laydate,
                layedit = layui.layedit,
                $ = layui.jquery;

            //验证规范性
            // form.verify({
            //     username: [
            //         /^[a-zA-Z]{6,12}$/,
            //         '用户名填写不规范！'
            //     ]
            // })

            //执行一个laydate实例
            laydate.render({
                elem: '#birthday' //指定元素
            });

            // 配置富文本编辑器的图片上传接口
            layedit.set({
                uploadImage: {
                    url: '/register/upload' //接口url
                    , type: '' //默认post
                }
            });

            var index = layedit.build('demo'); //建立编辑器





            //监听提交
            form.on('submit(formDemo)', function (data) {

                let tempArr = [];
                // 获取复选框中的值
                $("input[name='like']:checked").each(function () {
                    let res = $(this).val();
                    tempArr.push(res);
                })
                // 获取头像的值  只有一张图片时
                let poster = $("input[name='poster']")[0].files[0];

                // 获取富文本编辑器的内容
                let content = layedit.getContent(index);
                // 表单中其他的值
                let { username, password, mydate, sex } = data.field
                console.log(mydate);

                // 模拟表单
                let formData = new FormData();
                // 添加表单数据到formData
                formData.append('username', username);
                formData.append('password', password);
                formData.append('sex', sex);
                formData.append('mydate', mydate);
                formData.append('desc', content);
                formData.append('like', JSON.stringify(tempArr));
                formData.append('poster', poster)

                // 使用原生ajax发送数据
                $.ajax({
                    url: "/register/add",
                    method: 'post',
                    data: formData,
                    dataType: 'text',
                    contentType: false,
                    processData: false,
                    beforeSend: function () {
                        console.log('数据准备发送');
                    },
                    success: function (res) {
                        if (res == 'success') {
                            // 弹出框 数据注册成功，并跳转登录界面
                            layer.msg('注册成功');
                            setTimeout(() => {
                                let url = window.location.host + '/login'
                                console.log(url);
                                document.location.href = 'http://' + url
                            }, 1000)
                        }else{
                            layer.msg("数据保存失败")
                        }
                    },
                    error:function(err){
                        console.log(err);
                    }

                })


                return false;
            });



            form.render();
        });
    </script>
</body>

</html>